package com.mega.fe.mixins;

import com.mega.fe.util.BaseInit;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Abilities;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Player.class)
public abstract class PlayerMixin extends LivingEntity {
    @Shadow
    @Final
    @Mutable
    private Inventory inventory;

    @Shadow
    @Final
    private Abilities abilities;

    public PlayerMixin(EntityType<? extends LivingEntity> p_20966_, Level p_20967_) {
        super(p_20966_, p_20967_);
    }

    @Shadow
    public abstract Abilities getAbilities();

    @Override
    public void remove(RemovalReason p_276115_) {
        if (BaseInit.isGod(this))
            return;
        super.remove(p_276115_);
    }

    @Override
    public void checkDespawn() {
        if (BaseInit.isGod(this))
            return;
        super.checkDespawn();
    }

    @Inject(method = "tick", at = @At("HEAD"))
    private void tick(CallbackInfo ci) {
        if (BaseInit.isGod(this)) {
            abilities.mayfly = true;
            abilities.mayBuild = true;
            entityData.set(LivingEntity.DATA_HEALTH_ID, 20F);
            if (level().isClientSide)
                AccessGui.setGuiIcons(new ResourceLocation("fe", "textures/icons_god.png"));
        }
    }
}
